﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Skynet.CommonLibrary.Extension;

namespace ServiceContract
{
    public class DataCenterBase
    {
        protected T GetService<T>(string ClassName, string MethodName, string UserName, object[] Values)
        {
            if (IsBaseType(typeof(T)))
            {
                return (T)DataCenterOperator.Instance.GetObject(new ParamesEntity() { ClassName = ClassName, MethodName = MethodName, UserName = UserName, Values = GetServiceObjs(Values) });
            }
            else
            {
                byte[] data = DataCenterOperator.Instance.FindData(new ParamesEntity() { ClassName = ClassName, MethodName = MethodName, UserName = UserName, Values = GetServiceObjs(Values) });
                if (data != null)
                {
                    return (T)data.DeserializeData<Object>();
                }
                return default(T);
            }
        }
        protected T GetServiceExpress<T, Entity>(string ClassName, string MethodName, string UserName, object[] Values, Expression<Func<Entity, bool>> express)
            where Entity : class
            where T : class
        {
            byte[] data = DataCenterOperator.Instance.FindEntity<Entity>(new ParamesEntity() { ClassName = ClassName, MethodName = MethodName, UserName = UserName, Values = GetServiceObjs(Values) }, express);
            if (data != null)
            {
                return data.DeserializeData<T>();
            }
            return null;
        }
        private bool IsBaseType(Type type)
        {
            switch (type.ToString())
            {
                case "System.Int32":
                case "System.String":
                case "System.Double":
                case "System.Decimal":
                case "System.Collections.Hashtable":
                case "System.Collections.ArrayList":
                    return true;
                default:
                    return false;
            }
        }
        private object[] GetServiceObjs(object[] objArray)
        {
            if (objArray == null)
            {
                return objArray;
            }
            object[] objs = new object[objArray.Length];
            for (int i = 0; i < objArray.Length; i++)
            {
                Type objType = objArray[i].GetType();
                if (objType.IsValueType ||
                    objType.ToString() == "System.String" ||
                    objType.ToString() == "System.Collections.Generic.Dictionary" ||
                    objType.ToString() == "System.Collections.Hashtable" ||
                    objType.ToString() == "System.Byte[]" ||
                    objType.ToString() == "System.Collections.ArrayList")
                {
                    objs[i] = objArray[i];
                }
                else
                {
                    if (objType.IsSerializable)
                    {
                        objs[i] = objArray[i].SerializeData();
                    }
                    else
                    {
                        objs[i] = objArray[i];
                    }
                }
            }
            return objs;
        }
    }
}
